package com.dog.basics.service.mtkj.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.lang.Validator;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.dog.basics.bean.dto.api.mtkj.ApiGoodsSkuAddDto;
import com.dog.basics.bean.dto.api.mtkj.ApiGoodsSkuEditDto;
import com.dog.basics.bean.model.mtkj.MtGoods;
import com.dog.basics.bean.model.mtkj.MtGoodsSku;
import com.dog.basics.bean.model.mtkj.MtShopping;
import com.dog.basics.bean.vo.admin.mtkj.GoodSkuFromVo;
import com.dog.basics.bean.vo.api.mtkj.ApiGoodsSkuFromVo;
import com.dog.basics.common.util.PageParam;
import com.dog.basics.dao.mtkj.MtGoodsMapper;
import com.dog.basics.dao.mtkj.MtGoodsSkuMapper;
import com.dog.basics.dao.mtkj.MtShoppingMapper;
import com.dog.basics.security.util.SecurityUtils;
import com.dog.basics.service.mtkj.IMtGoodsSkuService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;


/**
 * <p>
 * 商品sku表 服务实现类
 * </p>
 *
 * @author wph
 * @since 2020-11-09
 */
@Service
@AllArgsConstructor
public class MtGoodsSkuServiceImpl extends ServiceImpl<MtGoodsSkuMapper, MtGoodsSku> implements IMtGoodsSkuService {

    private final MtGoodsMapper goodsMapper;
    private final MtShoppingMapper shoppingMapper;

    /**
     * 查询商品的所有sku
     * @param goodId
     * @return
     */
    @Override
    public IPage<ApiGoodsSkuFromVo> findGoodsSku(PageParam page , Long goodId) {
//
//        MtGoods goods = goodsMapper.selectById(goodId);
//        Validator.validateNotNull(goods,"商品信息不存在");
//
//        //查询商铺id检验是否是当前用户
//        MtShopping shopping = shoppingMapper.selectById(goods.getShopId());
//        Validator.validateNotNull(shopping,"商铺信息不存在");
//        Validator.validateEqual(shopping.getUserId(),SecurityUtils.getUser().getUserId(),"商铺信息不正确");
//        //判断是否属于该用户
//        Validator.validateEqual(shopping.getUserId(),SecurityUtils.getUser().getUserId(),"商铺信息有误");

        IPage iPage = this.page(page,Wrappers.<MtGoodsSku>lambdaQuery()
                .eq(MtGoodsSku::getGoodsId,goodId));
        List<MtGoodsSku> goodsSkuList = iPage.getRecords();
        List<ApiGoodsSkuFromVo> apiGoodsSkuFromVoList = goodsSkuList.stream().map(e -> BeanUtil.copyProperties(e, ApiGoodsSkuFromVo.class)).collect(Collectors.toList());
        iPage.setRecords(apiGoodsSkuFromVoList);
        return iPage;
    }

    /**
     * 新增商品规格
     *
     * @param: goodsSkuAdd
     * @returns
     * @author hangdy
     * @date: 2020/11/12 17:20
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean saveGoodsSku(ApiGoodsSkuAddDto goodsSkuAdd) {
        MtGoodsSku goodsSku = BeanUtil.copyProperties(goodsSkuAdd, MtGoodsSku.class);

        MtGoods goods = goodsMapper.selectById(goodsSkuAdd.getGoodsId());
        Validator.validateNotNull(goods,"商品信息不存在");

        //查询商铺id检验是否是当前用户
        MtShopping shopping = shoppingMapper.selectById(goods.getShopId());
        Validator.validateNotNull(shopping,"商铺信息不存在");
        Validator.validateEqual(shopping.getUserId(),SecurityUtils.getUser().getUserId(),"商铺信息不正确");
        //判断是否属于该用户
        Validator.validateEqual(shopping.getUserId(),SecurityUtils.getUser().getUserId(),"商铺信息有误");

        goodsSku.setCreateTime(DateUtil.currentSeconds());
        goodsSku.setCreateBy(SecurityUtils.getUser().getUserId());
        Validator.validateTrue(this.save(goodsSku), "新增商品规格失败");
        return Boolean.TRUE;
    }

    /**
     * 修改商品规格
     *
     * @param: goodsSkuEditDto
     * @returns
     * @author hangdy
     * @date: 2020/11/12 17:20
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean editGoodsSku(ApiGoodsSkuEditDto goodsSkuEditDto) {
        MtGoodsSku goodsSku = this.getById(goodsSkuEditDto.getId());
        Validator.validateNotNull(goodsSku, "未找到该商品规格");

        MtGoods goods = goodsMapper.selectById(goodsSkuEditDto.getGoodsId());
        Validator.validateNotNull(goods,"商品信息不存在");

        //查询商铺id检验是否是当前用户
        MtShopping shopping = shoppingMapper.selectById(goods.getShopId());
        Validator.validateNotNull(shopping,"商铺信息不存在");
        Validator.validateEqual(shopping.getUserId(),SecurityUtils.getUser().getUserId(),"商铺信息不正确");
        //判断是否属于该用户
        Validator.validateEqual(shopping.getUserId(),SecurityUtils.getUser().getUserId(),"商铺信息有误");

        BeanUtil.copyProperties(goodsSkuEditDto, MtGoodsSku.class);
        goodsSku.setUpdateBy(SecurityUtils.getUser().getUserId());
        goodsSku.setUpdateTime(DateUtil.currentSeconds());
        Validator.validateTrue(this.updateById(goodsSku), "跟新商品规格失败");
        return Boolean.TRUE;
    }

    /**
     * 删除商品规格
     *
     * @param: id
     * @returns
     * @author hangdy
     * @date: 2020/11/12 17:21
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean deleteGoodsSku(Long id) {
        MtGoodsSku goodsSku = this.getById(id);
        Validator.validateNotNull(goodsSku, "无此数据");

        MtGoods goods = goodsMapper.selectById(goodsSku.getGoodsId());
        Validator.validateNotNull(goods,"商品信息不存在");

        //查询商铺id检验是否是当前用户
        MtShopping shopping = shoppingMapper.selectById(goods.getShopId());
        Validator.validateNotNull(shopping,"商铺信息不存在");
        Validator.validateEqual(shopping.getUserId(),SecurityUtils.getUser().getUserId(),"商铺信息不正确");
        //判断是否属于该用户
        Validator.validateEqual(shopping.getUserId(),SecurityUtils.getUser().getUserId(),"商铺信息有误");

        Validator.validateTrue(this.removeById(id), "删除店铺商品类型失败");
        return Boolean.TRUE;
    }

    /**
     * admin服务
     *  根据商品id查询商品规格
     * @param page
     * @param goodId
     * @return
     */
    @Override
    public IPage<GoodSkuFromVo> adminFindGoodsSku(PageParam page, Long goodId) {

        IPage iPage = this.page(page,Wrappers.<MtGoodsSku>lambdaQuery()
                .eq(MtGoodsSku::getGoodsId,goodId));
        List<MtGoodsSku> goodsSkuList = iPage.getRecords();
        List<GoodSkuFromVo> apiGoodsSkuFromVoList = goodsSkuList.stream().map(e -> BeanUtil.copyProperties(e, GoodSkuFromVo.class)).collect(Collectors.toList());
        iPage.setRecords(apiGoodsSkuFromVoList);

        return iPage;
    }


    /**
     * admin服务
     *  根据商品id删除商品规格信息
     * @param id
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean adminDeleteGoodsSku(Long id) {

        MtGoodsSku goodsSku = this.getById(id);
        Validator.validateNotNull(goodsSku, "无此数据");

        Validator.validateTrue(this.removeById(id), "删除店铺商品规格信息失败");
        return Boolean.TRUE;

    }
}
